
<template>
  <el-dialog
      v-model="dialogVisible"
      title="项目详情"
      :close-on-click-modal="false"
      width="80%"
      @close="handleDialogClose"
  >
    <div  v-loading="loading">
      <div class="dialog-box">
        <div>
          <div class="dialog-one-title">项目信息</div>
          <el-descriptions :column="2" border>
            <el-descriptions-item label="项目名称" >{{projectInfo.name}}</el-descriptions-item>
            <el-descriptions-item label="批准经费">{{ turnWanyuan(projectInfo.feeAuthorize) }}</el-descriptions-item>
            <el-descriptions-item label="负责人">{{ projectInfo.chargerName }}</el-descriptions-item>
            <el-descriptions-item label="所属单位">{{ projectInfo.unitName }}</el-descriptions-item>
            <el-descriptions-item label="开始日期">{{ projectInfo.beginDate }}</el-descriptions-item>
            <el-descriptions-item label="计划完成日期">{{ projectInfo.planEndDate }}</el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
      <div>
        <el-table
            :data="tableData"
            style="width: 100%"
        >
          <el-table-column label="项目名称" sortable="p.name">
            <template #default="scope">
              <span @click="openDetails(scope.row)" class="clickable-status" >{{ scope.row.projectName }}</span>
            </template>
          </el-table-column>
          <el-table-column
              prop="chargerName"
              label="负责人"
              sortable="p.charger_name"
          ></el-table-column>
          <el-table-column
              prop="projectTypeName"
              label="项目分类"
              sortable="p.project_type"
          ></el-table-column>
          <el-table-column
              prop="alterdate"
              label="变更时间"
              sortable="b.alterdate"
          ></el-table-column>
          <el-table-column
              prop="unitName"
              label="所属单位"
              sortable="p.unit_id"
          ></el-table-column>
          <el-table-column  sortable="b.checkstatus" prop="checkstatus" label="审核状态">
            <template #default="{ row }">
              <CommonLabel
                  :dataList="checkStatusList"
                  valueKey="dictValue"
                  labelKey="dictLabel"
                  :value="row.checkstatus"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template #default="scope">
              <!-- 科研人员操作-->
              <el-button v-if="verifyRoleType.isKyry() && (scope.row.checkstatus == -1 || scope.row.checkstatus == 3)" plain type="primary" size="small" @click="handleEdit(scope.row)">
                编辑
              </el-button>
              <el-button v-if="verifyRoleType.isKyry() && (scope.row.checkstatus == -1 || scope.row.checkstatus == 3)" type="danger" size="small" plain @click="handleDelete(scope.row)">
                删除
              </el-button>
              <el-button v-if="verifyRoleType.isKyry() && scope.row.checkstatus == 2" @click="openDetails(scope.row)" plain type="primary" size="small">
                查看
              </el-button>
              <el-button v-if="verifyRoleType.isKyry() && scope.row.checkstatus == 1" plain type="warning" size="small" @click="handleCancel(scope.row)">
                撤销
              </el-button>

              <!-- 科研秘书操作 -->
              <el-button v-if="verifyRoleType.isKyms() && (scope.row.checkstatus == -1 || scope.row.checkstatus == 3)" plain type="primary" size="small" @click="handleEdit(scope.row)">
                编辑
              </el-button>
              <el-button v-if="verifyRoleType.isKyms() && (scope.row.checkstatus == -1 || scope.row.checkstatus == 3)" type="danger" size="small" plain @click="handleDelete(scope.row)">
                删除
              </el-button>
              <el-button v-if="verifyRoleType.isKyms() && scope.row.checkstatus == 1" plain type="primary" size="small" @click="handleEdit(scope.row)">
                编辑
              </el-button>
              <el-button v-if="verifyRoleType.isKyms() && scope.row.checkstatus == 1 && scope.row.isAuditor" plain type="success" size="small" @click="openDetails(scope.row , 'name')">
                审核
              </el-button>
              <el-button v-if="verifyRoleType.isKyms() && scope.row.checkstatus == 2" @click="openDetails(scope.row)" plain type="primary" size="small">
                查看
              </el-button>

              <!-- 科研管理员操作 -->
              <el-button v-if="verifyRoleType.iskycgly() && (scope.row.checkstatus == -1 || scope.row.checkstatus == 3)" plain type="primary" size="small" @click="handleEdit(scope.row)">
                编辑
              </el-button>
              <el-button v-if="verifyRoleType.iskycgly() && (scope.row.checkstatus == -1 || scope.row.checkstatus == 3)" @click="openDetails(scope.row)" plain type="primary" size="small">
                查看
              </el-button>
              <el-button v-if="verifyRoleType.iskycgly() && (scope.row.checkstatus == -1 || scope.row.checkstatus == 3)" type="danger" size="small" plain @click="handleDelete(scope.row)">
                删除
              </el-button>
              <el-button v-if="verifyRoleType.iskycgly() && scope.row.checkstatus == 1" plain type="primary" size="small" @click="handleEdit(scope.row)">
                编辑
              </el-button>
              <el-button v-if="verifyRoleType.iskycgly() && scope.row.checkstatus == 1" @click="openDetails(scope.row)" plain type="primary" size="small">
                查看
              </el-button>
              <el-button v-if="verifyRoleType.iskycgly() && scope.row.checkstatus == 1 && scope.row.isAuditor" plain type="success" size="small" @click="openDetails(scope.row, 'name')">
                审核
              </el-button>
              <el-button v-if="verifyRoleType.iskycgly() && scope.row.checkstatus == 2" @click="openDetails(scope.row)" plain type="primary" size="small">
                查看
              </el-button>


              <!--          <el-button  @click="woshizujian('7bcbfe53693141e9b9ef1030e6681944')">我是组件</el-button>-->
              <!--          <el-button  @click="openauditBtn(scope.row)" plain type="primary" size="small">cs审核</el-button>-->
              <!--          <el-button  v-if="!(verifyRoleType.isKymsOrkycgly() && scope.row.checkstatus == 2)" plain type="primary"  size="small" @click="handleEdit(scope.row)">编辑</el-button>-->
              <!--          <el-button  v-if="verifyRoleType.isKyry() && scope.row.checkstatus == '1'" plain type="warning" size="small" @click="handleCancel(scope.row)">撤销</el-button>-->
              <!--          <el-button v-if="scope.row.isAuditor"  plain type="success" size="small" @click="openDetails(scope.row)"  >审核</el-button>-->
              <!--          <el-button  v-if="scope.row.checkstatus == -1 || scope.row.checkstatus == 3" type="danger" size="small"  plain  @click="handleDelete(scope.row)">删除</el-button>-->
              <!--          <el-button v-if="scope.row.checkstatus ==2"  @click="openDetails(scope.row)"  plain type="primary" size="small" >查看</el-button>-->
            </template>
          </el-table-column>
        </el-table>
        <div class="example-pagination-block">
          <el-pagination
              layout="->,prev, pager, next, total"
              :total="count"
              currentPage1="10"
              @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

  </el-dialog>
  <detailsjfdzDialog
      ref="opendetailsjfdzDialog"
      @success="Getlist"
  >

  </detailsjfdzDialog>
  <compilejfdzDialog
      ref="opencompilejfdzDialog"
      @Getlist="Getlist"
  >

  </compilejfdzDialog>
</template>

<script setup lang="ts">
// 定义关闭事件回调
const emits = defineEmits(['emptyData']); // 定义上抛事件

// 对话框关闭时的处理函数
const handleDialogClose = () => {
  if (tableData.value.length === 0) {
    emits('emptyData'); // 表格数据为空时上抛事件
  }
};

import {
  selectVproject,
  projectOptions,
  list,
  geteditInfo,
  remove,

} from  '@/api/money/jfdzApi';
import compilejfdzDialog from '@/views/money/jfdz/compilejfdzDialog.vue';

import detailsjfdzDialog from '@/views/money/jfdz/jfdzDetails.vue';

import { verifyRoleType } from "@/utils/main";
import { turnWanyuan } from "@/utils/money";
import CommonLabel from "@/views/project/components/CommonLabel.vue";
import useDictFetch from "@/hooks/useDictFetch";
import {ref} from "vue";
const checkStatusList = useDictFetch("bpm_process_instance_status"); //审核状态
const dialogVisible = ref(false)
const loading = ref(false) // 添加加载状态
// 定义表格数据
const tableData = ref([])
//分页
const count = ref(0)
const handleCurrentChange = (val: number) => {
  list({
    projectId:projectInfo.value.projectId,
    pageNum:val,
    pageSize:10,
  })
      .then((res)=>{
        count.value = res.total
        tableData.value = res.rows

      })
}
const ID = ref('')
const openjftzsqTable = function (x){
  ID.value = x
  dialogVisible.value = true
  // getprojectOptions(x)
  getList(x)
  getprojectOptions(x)
  //alert('132')
}
const Getlist = function (){
  getList(ID.value)
  getprojectOptions(ID.value)
}
//获取项目下拉
const getprojectOptions = (x) => {
  selectVproject({
    pageNum:1,
    pageSize:50,
    projectId:x
  })
      .then((res)=>{
        projectInfo.value = res.rows[0]
      })
}
const getList = (row) => {
  list({
    projectId:row,
    pageNum:1,
    pageSize:10,
  })
      .then((res)=>{
        count.value = res.total
        tableData.value = res.rows

      })
}

// 项目名称下拉框选项
const projectOptions = ref([]);
const projectInfo = ref({
  projectId: "", // 项目下拉id
  projectName: "", // 项目，名称
  feeAuthorize: "",
  chargerName: "", // 负责人
  unitName: "", // 所属单位
  beginDate: "", // 开始事件
  planEndDate: "", // 计划结束事件
  isAuditor:false, //是否审核
  processInstanceId:'',//新加的行数据带来的id
  taskId:'',//新加的行数据带来的id
  feeChargerName: "", // 负责人名称
  feeCardNum: "", // 经费卡号
  incomeTotal: "", // 累计入账
  entryBalance: "",
  adjustmentFee: "0", // 调整量
  afterFee: "",
  alterreason: ""
});
// 获取项目下拉
const DialogTop = ref({});
// const getprojectOptions = (row) => {
//   geteditInfo({
//     Id: row,
//     //projectClass:itemObj.projectClass
//   }).then((res) => {
//     DialogTop.value = res.data
//     Object.assign(projectInfo.value, DialogTop.value, );
//   });
//
// };

//点击撤销
//撤销
import { cancelProcess } from "@/api/project/common";
import {ElMessage, ElMessageBox} from "element-plus";
const handleCancel = async (row) => {
  loading.value = true // 开始加载
  try {
    const res = await cancelProcess(row.processInstanceId);
    if (res.data) {
      ElMessage({
        message: "操作成功！",
        type: "success",
      });
      getList(ID.value)
    } else {
      ElMessage({
        message: "操作失败！",
        type: "warning",
      });
    }
  } catch (error) {
    console.log(error);
  } finally {
    loading.value = false // 结束加载
  }
};
// 处理删除操作
const handleDelete = (row) => {
  ElMessageBox.confirm(
      '确认！删除这条数据?',
      '提醒',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        loading.value = true // 开始加载
        remove(row.id)
            .then((res)=>{
              if(res.code == 200){
                ElMessage({
                  type: 'success',
                  message: res.msg,
                })
                emits('emptyData');
                getList(ID.value)
              }else {
                ElMessage({
                  type: 'info',
                  message: res.msg,
                })
              }
            })
            .finally(() => {
              loading.value = false // 结束加载
            })

      })
      .catch(() => {
        // ElMessage({
        //   type: 'info',
        //   message: 'Delete canceled',
        // })
      })

};
const compileRow = ref({})
//打开详情
const opendetailsjfdzDialog = ref(null)
const openDetails = (row , name) => {
  // 这里可以添加实际的编辑逻辑，比如弹出编辑表单等
  compileRow.value = row;
  opendetailsjfdzDialog.value.opencompileDialog(row , name);
};

//编辑
const opencompilejfdzDialog = ref(null);
// 处理编辑操作
const handleEdit = (row) => {
  // 这里可以添加实际的编辑逻辑，比如弹出编辑表单等
  compileRow.value = row;
  opencompilejfdzDialog.value.opencompileDialog(row);
};

// 暴露 openDialog 方法供父组件调用
defineExpose({
  openjftzsqTable
});
</script>
<style scoped lang="scss">

</style>